package com.bianguo.android.iocdemo.view.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

import com.bianguo.android.eventbuslibrary.utils.MLog;
import com.bianguo.android.iocdemo.R;
import com.bianguo.android.iocdemo.bean.BallBean;

import java.util.ArrayList;

public class SplitView extends View {

    public static final int DIAMETER = 30;
    private Paint mPaint;
    private Bitmap mBitmap;
    private ArrayList<BallBean> ballBeans;
    private float d = 3;
    private ValueAnimator animator;

    public SplitView(Context context) {
        this(context, null);
    }

    public SplitView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SplitView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tab_follow_selected);
        ballBeans = new ArrayList<>();
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                BallBean ballBean = new BallBean();
                ballBean.setColor(mBitmap.getPixel(i, j));
                ballBean.setX(i * d + d / 2);
                ballBean.setY(j * d + d / 2);
                ballBean.setR(d / 2);
                ballBean.setvX((float)(Math.pow(-1, Math.ceil(Math.random() * 1000)) * 20 * Math.random()));
                ballBean.setvY(rangInt(-15, 35));
                ballBean.setaX(0);
                ballBean.setaY(0.98f);
                ballBeans.add(ballBean);
            }
        }
        animator = ValueAnimator.ofFloat(0, 1);
        animator.setDuration(500);
        animator.setRepeatCount(-1);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(valueAnimator -> {
            updateBalls();
            invalidate();
        });
    }

    private int rangInt(int i, int j) {
        int max = Math.max(i, j);
        int min = Math.min(i, j);
        return (int) (min + Math.ceil(Math.random() * (max - min)));
    }

    private void updateBalls() {
        for (BallBean ballBean : ballBeans) {
            ballBean.setX(ballBean.getX() + ballBean.getvX());
            ballBean.setY(ballBean.getY() + ballBean.getvY());
            ballBean.setvX(ballBean.getvX() + ballBean.getaX());
            ballBean.setvY(ballBean.getvY() + ballBean.getaY());
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int desiredWidth = mBitmap.getWidth();
        int desiredHeight = mBitmap.getHeight();

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;

        //Measure Width
        if (widthMode == MeasureSpec.EXACTLY) {
            //Must be this size
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            //Can't be bigger than...
            width = Math.min(desiredWidth, widthSize);
        } else {
            //Be whatever you want
            width = desiredWidth;
        }

        //Measure Height
        if (heightMode == MeasureSpec.EXACTLY) {
            //Must be this size
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            //Can't be bigger than...
            height = Math.min(desiredHeight, heightSize);
        } else {
            //Be whatever you want
            height = desiredHeight;
        }

        //MUST CALL THIS
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(400, 0);
        for (BallBean ballBean : ballBeans) {
            mPaint.setColor(ballBean.getColor());
            canvas.drawCircle(ballBean.getX(), ballBean.getY(), ballBean.getR(), mPaint);
        }
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                animator.start();
                break;
        }
        return super.onTouchEvent(event);
    }
}
